package CB_C19;

import javax.media.*;
import javax.media.format.*;
import java.awt.Dimension;

/**Effect_Brightness
 * Created on 10.01.2010. <br>
 <br>
 Effect to adjust the brightness of the videos
 *<br>
 * @author Sebastian Czekierski, Stefan Blumberger
 *
 */

public class Effect_Brightness implements Effect {

    Format[] inputFormats;
    Format[] outputFormats;
    Format outputFormat;
    Format inputFormat;

    /**Brightness in percent*/
    double brightness = 1; //zwischen 1 und 0

    public Effect_Brightness(int in_b)
    {
        this.setbrightness(in_b);
        
        /**Convert*/
        outputFormats = new Format[]
        {
                new RGBFormat
                (
                null,
                Format.NOT_SPECIFIED,
                Format.intArray,
                Format.NOT_SPECIFIED, 32,
                Format.NOT_SPECIFIED,
                Format.NOT_SPECIFIED,
                Format.NOT_SPECIFIED
                )
        };
        inputFormats = new Format[]
        {
                new RGBFormat
                (
                null,
                Format.NOT_SPECIFIED,
                Format.intArray,
                Format.NOT_SPECIFIED, 32,
                Format.NOT_SPECIFIED,
                Format.NOT_SPECIFIED,
                Format.NOT_SPECIFIED
                )
        };
    }
    public Format[] getSupportedInputFormats() {
        //System.out.println("getinputFormat");
        return inputFormats;
    }

    public Format[] getSupportedOutputFormats(Format input) {
        if (input == null) {
            return (outputFormats);
        }
        if (input instanceof RGBFormat) {
            return (new Format[]{
                        input,
                        outputFormats[0]
                    });
            
        } else {
            
            return (outputFormats);
        }
    }

    public Format setInputFormat(Format input) {
        if (input instanceof RGBFormat) {
            //System.out.println("setinputFormat to input");
            this.inputFormat = (RGBFormat) input;
            return inputFormat;
        } else {
            //System.out.println("setinputFormat inputFormats[0]");
            return null;
        }
        
    }

    public Format setOutputFormat(Format output) {
        if (output instanceof RGBFormat) {
            //System.out.println("setOutputFormat to output");
            outputFormat = (RGBFormat) output;
        } else {
            outputFormat = (RGBFormat) outputFormats[0];
            //System.out.println("setOutputFormat outputFormats[0]");
        }
        return (outputFormat);
    }

   /**processes one frame at a time and adjusts its brightness*/
    public int process(Buffer in, Buffer out)
    {
        out.copy(in,true);

        /**get RGB Mask*/
        RGBFormat inFormat = (RGBFormat)in.getFormat();
        int redMask = inFormat.getRedMask();
        int greenMask = inFormat.getGreenMask();
        int blueMask = inFormat.getBlueMask();

        /**Get Size of image*/
        Dimension inSize = inFormat.getSize();
        int w = (int) inSize.getWidth();
        int h = (int) inSize.getHeight();
        int y,x,PixelWert,offBuffer;
        /**Set Color*/
        int farbe = 0x00000000;

        /**Store out Buffer in an int Array*/
        int[] data = (int[]) out.getData();

        /**process all Pixels*/
        for(y=0; y<h; y++)
        {
            for(x=0; x<w; x++)
            {
                /**curent pixel*/
                offBuffer = y*w + x;
                /**Set new Color*/
                PixelWert = ((int)((data[offBuffer] & redMask) *   (1-brightness) + farbe *(brightness))) & redMask;
                PixelWert +=((int)((data[offBuffer] & greenMask) * (1-brightness) + farbe *(brightness))) & greenMask;
                PixelWert +=((int)((data[offBuffer] & blueMask) *  (1-brightness) + farbe *(brightness))) & blueMask;
                data[offBuffer] = PixelWert;
            }
       }
       /**Put Pixel into Out Buffer*/
       out.setData(data);

       return BUFFER_PROCESSED_OK;
    }

    public String getName() {
        return "Effect_anzahl";
    }

    public void open() 
    {
    }

    public void close() {
    }

    public void reset() {
    }

    public Object[] getControls() {
        return null;
    }

    public Object getControl(String arg0) {
            return this;
    }
    public void setbrightness(int in_b)
    {
        brightness = (double)in_b;
        this.brightness = 1-(brightness/100);
    }
}
